package game

import (
	"github.com/hwcer/cosgo/registry"
	"github.com/hwcer/yyds/context"
	"server/game/define/helper"
	"server/game/handle"
	"server/game/model"
	"server/game/pkg/chat"
	"server/share/config"
)

// 启用短链接聊天
func init() {
	handle.Register(&Chat{})
}

type Chat struct {
}

func (this *Chat) Caller(node *registry.Node, handle *context.Context) interface{} {
	f := node.Method().(func(*Chat, *context.Context) interface{})
	return f(this, handle)
}

// Send 发送聊天
//
// msg string 聊天内容 200 字节
func (this *Chat) Send(c *context.Context) interface{} {
	text := c.GetString("text")
	//notify := helper.NewNotify(c.Player.Updater)

	doc := c.Player.Document(config.ITypeRole)
	role := doc.Any().(*model.Role)
	args := map[string]any{
		"lv":   role.Lv,
		"name": role.Name,
	}
	if _, err := chat.Send(role.Uid, text, args, nil); err != nil {
		return err
	}
	return true
}

// Getter 获取最新聊天记录
//
// 返回 []*chat.Message
func (this *Chat) Getter(c *context.Context) interface{} {
	size := c.GetInt32("size")
	if size < 10 {
		size = 10
	} else if size > 100 {
		size = 100
	}
	notify := helper.NewNotify(c.Player.Updater)
	var rows []*chat.Message
	notify.Chat, rows = chat.Getter(notify.Chat, int(size), nil)
	return rows
}
